package com.example.springbootdemo.utils;

import com.huaban.analysis.jieba.JiebaSegmenter;

import java.util.*;

import org.apache.commons.text.similarity.JaccardSimilarity;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ParagraphSimilarityExample {

    public static Double getSimilarity(String[] text1,String[] text2) {

        double similarity = calculateParagraphSimilarity(text1, text2);
//        double similarityThreshold = 0.3; // Set your similarity threshold here
/*
        if (similarity >= similarityThreshold) {
            System.out.println("The two texts have similar paragraphs.");
        } else {
            System.out.println("The two texts do not have similar paragraphs.");
        }*/
        return similarity;
    }

    private static double calculateParagraphSimilarity(String[] paragraphs1, String[] paragraphs2 ) {

        double totalSimilarity = 0.0;

        for (String paragraph1 : paragraphs1) {
            for (String paragraph2 : paragraphs2) {
                double paragraphSimilarity = calculateJaccardSimilarity(paragraph1, paragraph2);
                totalSimilarity += paragraphSimilarity;
            }
        }

        int totalComparisons = paragraphs1.length * paragraphs2.length;

        return totalSimilarity / totalComparisons;
    }
//    private static double calculateParagraphSimilarity(String text1, String text2) {
//        String[] paragraphs1 = text1.split("\n");
//        String[] paragraphs2 = text2.split("\n");
//
//        double totalSimilarity = 0.0;
//
//        for (String paragraph1 : paragraphs1) {
//            for (String paragraph2 : paragraphs2) {
//                double paragraphSimilarity = calculateJaccardSimilarity(paragraph1, paragraph2);
//                totalSimilarity += paragraphSimilarity;
//            }
//        }
//
//        int totalComparisons = paragraphs1.length * paragraphs2.length;
//
//        return totalSimilarity / totalComparisons;
//    }

    private static double calculateJaccardSimilarity(String paragraph1, String paragraph2) {
        Set<String> set1 = createSetFromParagraph(paragraph1);
        Set<String> set2 = createSetFromParagraph(paragraph2);
//        Iterator<String> iterator = set2.iterator();
/*        int i=0;
        while (iterator.hasNext()) {
            String item = iterator.next();
        }*/
        return jaccardSimilarity(set1, set2);
//        return 0.0;
    }

    private static Set<String> createSetFromParagraph(String paragraph) {
        Set<String> wordSet = new HashSet<>();
        wordSet.add(paragraph); // Treat the whole paragraph as a single element
        return wordSet;
    }

    public static double jaccardSimilarity(Set<String> set1, Set<String> set2) {
        JaccardSimilarity jaccardSimilarity = new JaccardSimilarity();
        String s1 = convertToString(convertSetToCharSequenceArray(set1));
        String s2 = convertToString(convertSetToCharSequenceArray(set2));
        return jaccardSimilarity.apply(s1, s2);
    }

    private static CharSequence[] convertSetToCharSequenceArray(Set<String> set) {
        CharSequence[] charSequences = new CharSequence[set.size()];
        int index = 0;
        for (String str : set) {
            charSequences[index++] = str;
        }
        return charSequences;
    }
    private static String convertToString(CharSequence[] charSequenceArray) {
        return Arrays.stream(charSequenceArray)
                .map(CharSequence::toString)
                .collect(Collectors.joining(", "));
    }
}
